home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Toolbox / ProgressBars 1.0 / Sources / NonThreadedProgress.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-17  |  4.0 KB  |  215 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        NonThreadedProgress.c
  3.  
  4.     Contains:    Progress bar implementation without using the Threads Manager
  5.  
  6.     Written by:    Chris White, Developer Technical Support
  7.     
  8.     Copyright:    © 1996 by Apple Computer, Inc., all rights reserved.
  9.     
  10.     Change History (most recent first):
  11.     
  12.             1/22/96            CW        First release
  13.  
  14. */
  15.  
  16.  
  17. #pragma segment Core
  18.  
  19.  
  20. // System Includes
  21.  
  22. #ifndef __DIALOGS__
  23.     #include <Dialogs.h>
  24. #endif
  25.  
  26.  
  27.  
  28. // Application Includes
  29.  
  30. #ifndef __BAREBONES__
  31.     #include "BareBones.h"
  32. #endif
  33.  
  34. #ifndef __PROTOTYPES__
  35.     #include "ProtoTypes.h"
  36. #endif
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43. // static prototypes
  44. static void        DrawProgressBar ( DialogRef theDialog, int doneAmount, int maxAmount );
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. OSErr ProgressOperation ( tOperation theOperation, void* refCon, StringPtr theText )
  52. {
  53.     Boolean            bCancelled = false;
  54.     OSErr            theErr = noErr;
  55.     SInt16            theType;
  56.     GrafPtr            savePort;
  57.     DialogRef        theDialog = nil;
  58.     Handle            theHan;
  59.     Rect            theRect;
  60.     
  61.     
  62.     theDialog = GetNewDialog ( kProgressDialogID, nil, (WindowPtr) -1 );
  63.     
  64.     GetPort ( &savePort );
  65.     SetPort ( theDialog );
  66.     
  67.     GetDialogItem ( theDialog, kStaticTextItemID, &theType, &theHan, &theRect );
  68.     SetDialogItemText ( theHan, theText );
  69.     
  70.     ShowWindow ( theDialog );
  71.     DrawDialog ( theDialog );
  72.     
  73.     bCancelled = (*theOperation) ( refCon, theDialog );
  74.     
  75.     DisposeDialog ( theDialog );
  76.     SetPort ( savePort );
  77.     
  78.     return theErr;
  79.     
  80. } // ProgressOperation
  81.  
  82.  
  83.  
  84. Boolean UpdateProgressBar ( DialogRef theDialog, int doneAmount, int maxAmount )
  85. {
  86.     Boolean            bCancelled = false;
  87.     OSErr            theErr = noErr;
  88.     EventRecord        theEvent;
  89.     
  90.     
  91.     
  92.     if ( WaitNextEvent ( everyEvent - diskMask, &theEvent, kSleepTime, nil ) )
  93.     {
  94.         if ( IsDialogEvent ( &theEvent ) )
  95.         {
  96.             SInt16        itemHit;
  97.             DialogRef    whichDialog;
  98.             
  99.             if ( DialogSelect ( &theEvent, &whichDialog, &itemHit ) )
  100.             {
  101.                 if ( whichDialog == theDialog && itemHit == kCancelItemID )
  102.                     bCancelled = true;
  103.             }
  104.         }
  105.         else if ( theEvent.what == mouseDown )
  106.         {
  107.             WindowRef    theWindow;
  108.             
  109.             if ( FindWindow ( theEvent.where, &theWindow ) == inDrag )
  110.                 DoDragWindow ( theWindow, &theEvent );
  111.         }
  112.     }
  113.     
  114.     DrawProgressBar ( theDialog, doneAmount, maxAmount );
  115.     
  116.     return bCancelled;
  117.     
  118. } // UpdateProgressBar
  119.  
  120.  
  121.  
  122. static void DrawProgressBar ( DialogRef theDialog, int doneAmount, int maxAmount )
  123. {
  124.     SInt16            theType;
  125.     int                theLength;
  126.     float            floatDone, floatMax, thePercent;
  127.     Handle            theHan;
  128.     Rect            theRect;
  129.     
  130.     
  131.     
  132.     GetDialogItem ( theDialog, kUserItemID, &theType, &theHan, &theRect );
  133.     if ( maxAmount )        // Standard Progress Bar
  134.     {
  135.         SetDialogItem ( theDialog, kUserItemID, theType, (Handle) gOutlineUserItemUPP, &theRect );
  136.         // call it now, so that after an update event the boarder
  137.         // is drawn before FillRect is called
  138.         CallUserItemProc ( gOutlineUserItemUPP, theDialog, kUserItemID );
  139.         
  140.         
  141.         theLength = theRect.right - theRect.left;
  142.         
  143.         floatDone = doneAmount;
  144.         floatMax = maxAmount;
  145.         thePercent = (floatDone / floatMax) * 100;
  146.         theRect.right = theRect.left + ((thePercent / 100) * theLength);
  147.         FillRect ( &theRect, &qd.black );
  148.     }
  149.     else                    // Barber Pole Progress Bar
  150.     {
  151.         static SInt16    theID = 1000;
  152.         PicHandle        thePic;
  153.         
  154.         
  155.         thePic = GetPicture ( theID++ );
  156.         DrawPicture ( thePic, &theRect );
  157.         if ( theID > 1003 )
  158.             theID = 1000;
  159.     }
  160.     
  161.     return;
  162.     
  163. } // DrawProgressBar
  164.  
  165.  
  166.  
  167. //
  168. // This routine is one of the operations carried out
  169. // which the progress bar is representing.
  170. //
  171. Boolean StandardDemoOperation ( void* refCon, DialogRef theDialog )
  172. {
  173.     Boolean        bWasCancelled = false;
  174.     int            i;
  175.     const int    max = 100;
  176.     
  177.     
  178.     for ( i = 1; i <= max && bWasCancelled == false; i++ )
  179.     {
  180.         SInt32    theDelay = 10L;
  181.         Delay ( theDelay, &theDelay );
  182.         bWasCancelled = UpdateProgressBar ( theDialog, i, max );
  183.     }
  184.     
  185.     return bWasCancelled;
  186. }
  187.  
  188.  
  189.  
  190. //
  191. // This routine is one of the operations carried out
  192. // which the progress bar is representing.
  193. //
  194. Boolean BarberPoleDemoOperation ( void* refCon, DialogRef theDialog )
  195. {
  196.     Boolean        bWasCancelled = false;
  197.     int            i;
  198.     const int    max = 100;
  199.     
  200.     
  201.     for ( i = 1; i <= max && bWasCancelled == false; i++ )
  202.     {
  203.         SInt32    theDelay = 10L;
  204.         Delay ( theDelay, &theDelay );
  205.         bWasCancelled = UpdateProgressBar ( theDialog, 0, 0 );
  206.     }
  207.     
  208.     return bWasCancelled;
  209. }
  210.  
  211.  
  212.  
  213.  
  214.  
  215.